<template>
  <div class="caigouruku-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>        
        <el-button plain type="primary" @click="toPrint">
          <vab-icon icon="printer-line" />
          打印
        </el-button>
        <el-button plain type="primary" @click="toShowRelated">
          <vab-icon icon="file-list-2-line" />
          拆装箱明细
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :data="barcodeXcList"
      highlight-current-row
      @current-change="handleCurrentChange"      
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column align="left" label="存货编码名称" width="220">
        <template #default="scope">
          <p style="margin: 0px">
            {{ scope.row.invcode }}
          </p>
          <p style="margin: 0px; font-weight: 600">
            {{ scope.row.invname }}
          </p>
        </template>
      </el-table-column>
      <el-table-column align="left" label="批次号" prop="invbatch" width="180" />
      <el-table-column align="left" label="规格" prop="invspec" width="180" />
      <el-table-column align="left" label="型号" prop="invtype" width="180" />
      <el-table-column align="left" label="类型" prop="barcodeType" width="180" />
      <el-table-column align="left" label="仓库" prop="whName" width="180" />
      <el-table-column align="left" label="数量" width="150">
        <template #default="scope">
          <p style="margin: 0px; font-weight: 600; color: #3366ff">
            {{ scope.row.xcQty }} {{ scope.row.measName }}
          </p>
        </template>
      </el-table-column>
      <el-table-column align="left" label="创建信息" width="220">
        <template #default="scope">
          <p style="margin: 0px">
            {{ scope.row.createUser }}
          </p>
          <p style="margin: 0px; font-weight: 600">
            {{ scope.row.createTime }}
          </p>
        </template>
      </el-table-column>
      <el-table-column align="left" label="拆箱批次" prop="unboxInvbatch" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
    <RelatedModuleVue ref="relatedModuleRef" />
  </div>
</template>

<script setup>
  import { Search } from '@element-plus/icons-vue'
  import { getBarcodeXcPageApi } from '@/api/kc/barcode_xc'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'
  import RelatedModuleVue from './modules/relatedModule.vue'
  import hiPrint from '@/utils/hiPrint.js'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const queryForm = ref({ pkCorp: pkCorp })
  const barcodeXcList = ref()
  const barcodeXc = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
  })

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  const queryDialogRef = ref()
  const relatedModuleRef = ref()
  const selectedRows = ref()

  const getBarcodeXcList = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...queryForm.value,
        ...paginationParams,
        beginDate:
          queryForm.value.queryDate === undefined
            ? ''
            : queryForm.value.queryDate[0],
        endDate:
          queryForm.value.queryDate === undefined
            ? ''
            : queryForm.value.queryDate[1],
      }
      const { code, msg, data } = await getBarcodeXcPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        barcodeXcList.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => {
    queryDialogRef.value.openDialog()
  }

  const queryCallback = (value) => {
    queryForm.value = value
    getBarcodeXcList()
  }

  const handleCurrentChange = (row) => {
    barcodeXc.value = row
  }
  const toShowRelated = () => {
    if (barcodeXc.value!=undefined && barcodeXc.value.pkBarcodeXc) {
      relatedModuleRef.value.toOpenRelated(barcodeXc.value)
    } else {
      $baseMessage('请选择需要查看的拆装箱信息', 'warning')
    }
  }
  const handleSelectionChange = (rows) => {
    selectedRows.value = rows
  }
  const toPrint = () => {
    hiPrint('bar_001', selectedRows.value)
  }
  watch(paginationParams, () => getBarcodeXcList())

  onMounted(() => getBarcodeXcList())
</script>
